@extends('layouts.app')

@section('content')
<div class="content content_active">
	<div class="button">
		<button class="add_activity">添加活动</button>
	</div>
	<table>
		<thead>
			<tr>
				<th>活动名称</th><th>创建时间</th><th>活动时长</th><th>活动状态</th><th>活动时间</th><th>是否导入订单</th><th>操作</th>
			</tr>
		</thead>
		<tbody id="list">
		</tbody>
	</table>
</div>
@endsection
@section('scripts')
<script type="text/javascript">
	params['page'] = 1;
	params['pageSize'] = 10;
    getList('activitys',params);
	$(function(){
		$('.page_content').on('click','.add_activity',function(){
			$.ajax({
		        type: "GET",
		        url: "/activitys/create",
		        dataType: "html",
		        success: function(res) {
					art.dialog({
						title: '添加活动',
						width: '497px',
						lock: true,
						id: 'edit',
						content: res,
					});
				}
			});
		})

		$('#list').on('click','#edit',function(){
			var id = $(this).attr('data-id');
			$.ajax({
		        type: "GET",
		        url: "/activitys/edit/"+id,
		        dataType: "html",
		        success: function(res) {
					art.dialog({
						title: '添加商品',
						width: '497px',
						lock: true,
						id: 'edit',
						content: res,
					});
				}
			});
		})

		$('#list').on('click','#del',function(){
			var id = $(this).prev().attr('data-id');
			$.ajax({
		        type: "DELETE",
		        url: "/activity/"+id,
		        dataType: "json",
		        success: function(res) {
		        	box.tipHover({ status: 0, msg: "删除成功" });
		        	// alert('删除成功');
    				getList('activitys',params);
				}
			});
		})

		$('body').on('click','.switch',function() {
			var status = $(this).attr('data-status');
			var id = $(this).parent().next().find('#edit').attr('data-id');
			$.ajax({
		        type: "get",
		        url: "/activity/changeStatus/"+id,
		        dataType: "json",
		        success: function(res) {
				}
			});
		})

		$("body").on("click",".true",function(){
			var form = new Object();
			form.name = $('#name').val();
			form.product = $('#product').val();
			form.days = $('#days').val();
			form.image = $('.upload_img').attr("data-img");
			var id = $(this).attr('data-id');
			if(id != '') {
				form.id = id;
			}
			if(form.image == undefined || form.image == "") {
				box.tipHover({ status: 0, msg: "活动图片不能为空" });
				// alert('活动图片不能为空');
				return false;
			}
			if(form.name == "") {
				box.tipHover({ status: 0, msg: "活动名称不能为空" });
				// alert('活动名称不能为空');
				return false;
			}
			if(form.days == "" || form.days < 7) {
				box.tipHover({ status: 0, msg: "活动时长不能为空且不能少于7天" });
				// alert('活动时长不能为空且不能少于7天');
				return false;
			}
			if(form.product == "") {
				box.tipHover({ status: 0, msg: "活动商品不能为空" });
				// alert('活动商品不能为空');
				return false;
			}
			$.ajax({
		        type: "POST",
		        url: "/activity",
		        dataType: "json",
		        data: form,
		        success: function(res) {
		        	art.dialog.list['edit'].close();
    				getList('activitys',params);
				},
				error:function(res){
					box.tipHover({ status: 0, msg: "活动名称已存在！" });
					// alert('活动名称已存在！')
				}

			});
		})
	})

	function uploadPic(){
        var pic = $('#upload_file')[0].files[0];
        var from = new FormData();
        from.append('file', pic);
        $.ajax({
            url:"/uploadImage",
            type:"post",
            data: from,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success:function(data){
            	var img = $('.upload_img');
                img.css("background",'url('+data.data+') no-repeat center').css("background-size","cover");
                img.attr("data-img",data.data);
            }
        });
    }
</script>
@endsection